﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using olierFrm.DataBase.FrameWork.Plugin;
using System.Web.Routing;

namespace olierFrm.MVC
{
    public class CustomMvcRouteHandler : IRouteHandler
    {

        /*  public System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext)
          {
              string controllerName = requestContext.RouteData.GetRequiredString("controller");
              if (controllerName.IndexOf(".ico") != -1 || controllerName.IndexOf(".gif") != -1) return null;

              requestContext.RouteData.Values.Add("Module", "");
              requestContext.RouteData.Values.Add("CustController", "");
              IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
              bool haveControllerMethod = true;
              IController controller;
              try
              {
                  controller = factory.CreateController(requestContext, controllerName);
                  if (controller.GetType().GetMethod(requestContext.RouteData.GetRequiredString("action")) == null)
                  {
                      haveControllerMethod = false;
                  }
                  else
                  {
                      if (controller.GetType().GetMethod(requestContext.RouteData.GetRequiredString("action")) == null && requestContext.RouteData.GetRequiredString("action") != "Index" && requestContext.RouteData.Values["Params"].ToString() == "")
                      {
                          requestContext.RouteData.Values["Params"] = requestContext.RouteData.GetRequiredString("action");
                          requestContext.RouteData.Values["action"] = "Index";
                      }
                      requestContext.RouteData.Values["Module"] = requestContext.RouteData.Values["action"];
                      requestContext.RouteData.Values["CustController"] = controllerName;
                  }
              }
              catch
              {
                  haveControllerMethod = false;
              }
              if (!haveControllerMethod)
              {
                  if (requestContext.RouteData.GetRequiredString("action") != "Index" && requestContext.RouteData.Values["Params"].ToString() == "")
                  {
                      ReceiptsConfig rc = new ReceiptsConfig(controllerName + "/" + requestContext.RouteData.Values["action"]);
                      if (rc.IniScript == "")
                      {
                          try
                          {
                              requestContext.RouteData.Values["Params"] = requestContext.RouteData.GetRequiredString("params");
                          }
                          catch
                          {
                          }
                          requestContext.RouteData.Values["Module"] = requestContext.RouteData.GetRequiredString("action");
                      }
                      else
                      {
                          requestContext.RouteData.Values["Module"] = requestContext.RouteData.Values["action"];
                      }
                  }
                  else
                  {
                      requestContext.RouteData.Values["Module"] = requestContext.RouteData.GetRequiredString("action");
                  }
                  requestContext.RouteData.Values["controller"] = "Framework";
                  requestContext.RouteData.Values["CustController"] = controllerName;
                  requestContext.RouteData.Values["action"] = "Index";
                  controllerName = "Framework";
                  requestContext.RouteData.Values["controller"] = controllerName;
              }
              controller = factory.CreateController(requestContext, controllerName);
              if (controller == null)
              {
                  throw new System.InvalidOperationException();
              }
              return new CustomMvcHandler(controller, factory, requestContext);
          }*/

        public System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            string controllerName = requestContext.RouteData.GetRequiredString("controller");
            if (controllerName.IndexOf(".ico") != -1 || controllerName.IndexOf(".jpg") != -1 || controllerName.IndexOf(".gif") != -1) return null;
            requestContext.RouteData.Values.Add("Module", "");
            requestContext.RouteData.Values.Add("CustController", "");
            string CustController = controllerName;
            string Module = requestContext.RouteData.GetRequiredString("action");
            IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
            bool haveControllerMethod = false;
            IController controller;
            try
            {
                controller = factory.CreateController(requestContext, controllerName);
                var methods = controller.GetType().GetMethods().Where(m => m.Name.ToLower() == requestContext.RouteData.GetRequiredString("action").ToLower());
                haveControllerMethod = methods.Any();
                //var methods = controller.GetType().GetMethods();
                //foreach (var m in methods)
                //{
                //    if (m.Name.ToLower() == requestContext.RouteData.GetRequiredString("action").ToLower())
                //    {
                //        haveControllerMethod = true;
                //        continue;
                //    }
                //}

                if (!haveControllerMethod)
                {

                    if (!methods.Any() && requestContext.RouteData.GetRequiredString("action").ToLower() != "index" && requestContext.RouteData.Values["Params"].ToString() == "")
                    {
                        requestContext.RouteData.Values["Params"] = requestContext.RouteData.GetRequiredString("action");
                        requestContext.RouteData.Values["action"] = "index";
                    }
                    //Module = requestContext.RouteData.Values["action"].ToString();
                    CustController = controllerName;
                }
            }
            catch
            {
                haveControllerMethod = false;
            }
            if (!haveControllerMethod)
            {
                if (requestContext.RouteData.GetRequiredString("action") != "Index" && requestContext.RouteData.Values["Params"].ToString() == "")
                {
                    ReceiptsConfig rc = new ReceiptsConfig(controllerName + "/" + requestContext.RouteData.Values["action"]);

                    //ReceiptsConfig rc = new ReceiptsConfig(controllerName + "/" + requestContext.RouteData.Values["action"]);
                    if (rc.IniScript.Trim() == "")
                    {
                        try
                        {
                            requestContext.RouteData.Values["Params"] = requestContext.RouteData.GetRequiredString("params");
                        }
                        catch
                        {
                        }
                    }
                    Module = requestContext.RouteData.GetRequiredString("action");
                }
                requestContext.RouteData.Values["controller"] = "Framework";
                requestContext.RouteData.Values["action"] = "Index";

            }
            requestContext.RouteData.Values["CustController"] = CustController;
            requestContext.RouteData.Values["Module"] = Module;

            controller = factory.CreateController(requestContext, requestContext.RouteData.GetRequiredString("controller"));
            if (controller == null)
            {
                throw new System.InvalidOperationException();
            }
            return new CustomMvcHandler(controller, factory, requestContext);
        }
    }
}
